package com.hsnn.medstgmini.base.dict.controller;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.dict.service.DicEntProfileLogManager;
import com.hsnn.medstgmini.util.Pagination;


/**
 * @category 企业资质 类型日志
 * @author wangbing
 * @date 2015年7月13日
 */
@Controller
@RequestMapping("/dicEntProfileLog")
public class DicEntProfileLogController {

	private static final Logger log = Logger.getLogger(DicEntProfileLogController.class);
			

	@Autowired
	private DicEntProfileLogManager dicEntProfileLogManager;

	@Autowired
	private HttpServletRequest request;
	
	
	/**
	 * @category 跳转企业资质 类型日志页面
	 * @date 2015年7月13日
	 * @return String
	 */
	@RequestMapping("/log")
	public String toEntProfileLogList() {
		log.info("enter toEntProfileLogList method");
		String dicEntProfileId = request.getParameter("dicEntProfileId");
		request.setAttribute("dicEntProfileId", dicEntProfileId);;
		request.setAttribute("firstLevelIndiCode", request.getParameter("firstLevelIndiCode"));
		try {
			String firstLevelIndiName = new String(request.getParameter("firstLevelIndiName").getBytes("ISO8859-1"),"utf-8");
			request.setAttribute("firstLevelIndiName", firstLevelIndiName);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		
		
		request.setAttribute("dicEntProfileId", request.getParameter("dicEntProfileId"));
		return "/base/dict/entProfileLogList";
	}
	
	/**
	 * @category 获取企业资质 类型日志列表
	 * @date 2015年7月13日
	 * @return page
	 */
	@RequestMapping("/getDicEntProfileLogList")
	@ResponseBody
	public Pagination getDicEntProfileLogList(){
		log.info("enter getDicEntProfileLogList method");
		Pagination page = new Pagination(request);
		try {
			page.getConditions().put("dicEntProfileId", request.getParameter("dicEntProfileId"));
			page.getConditions().put("startDate", request.getParameter("startDate"));
			page.getConditions().put("endDate", request.getParameter("endDate"));
			dicEntProfileLogManager.getDicEntProfileLogList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 跳转企业资质 类型日志详情页面
	 * @date 2015年7月13日
	 * @return String
	 */
	@RequestMapping("/toEntProfileLogDetail")
	public String toEntProfileLogDetail() {
		log.info("enter toEntProfileLogDetail method");
		try {
			List<Map<String,Object>> list = dicEntProfileLogManager.getDicEntProfileLogDetail(request.getParameter("logId"));
			List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
			String[] cColunm = {"企业资质类型编码","企业资质类型名称","状态","备注"};
			String[] eColunm = {"firstLevelIndiCode","firstLevelIndiName","status","remark"};
			for(int i = 0;i<cColunm.length;i++){
				Map<String,Object> map =  new HashMap<String,Object>();
				map.put("colunm", cColunm[i]);//字段名
				map.put("after", !"status".equals(eColunm[i])?list.get(0).get(eColunm[i]):"1".equals(list.get(0).get(eColunm[i]).toString())?"启用":"停用");//修改后数据
				map.put("before", list.size()==1?null:!"status".equals(eColunm[i])?list.get(1).get(eColunm[i]):"1".equals(list.get(1).get(eColunm[i]).toString())?"启用":"停用");//修改前数据
				if(list.size()==1){
					map.put("red", "1");//是否变红加粗 1是 0 否
				}else{
					//判断修改后与修改前数据是否相等
					if(list.get(0).get(eColunm[i])!=null&&list.get(1).get(eColunm[i])!=null&&!list.get(0).get(eColunm[i]).toString().equals(list.get(1).get(eColunm[i]).toString())){
						map.put("red", "1");
					}else{
						map.put("red", "0");
					}
				}
				mapList.add(map);
			}
			for(int i = 0;i<mapList.size();i++){//把要变红的放在list前面
				if("1".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			for(int i = 0;i<mapList.size();i++){
				if("0".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			request.setAttribute("list", list);
			request.setAttribute("dataList", dataList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/base/dict/entProfileLogDetail";
	}
}
